update tasking 2

Documentation Version for Comments and Changes

You are invited to make any changes...add any comments.

Changes will `eventually` be merged into the offical documentation.

Leave any commnents here...

...

... back to index page OE documentation



return x = 0 or x = 1 end type

boolean t1_running, t2_running

procedure task1(sequence message) for i = 1 to 10 do printf(1, "task1 (%d) %s\n", {i, message}) task_yield() end for t1_running = FALSE end procedure

procedure task2(sequence message) for i = 1 to 10 do printf(1, "task2 (%d) %s\n", {i, message}) task_yield() end for t2_running = FALSE end procedure

puts(1, "main task: start\n")

atom t1, t2

t1 = task_create(routine_id("task1"), {"Hello"}) t2 = task_create(routine_id("task2"), {"Goodbye"})

task_schedule(t1, {2.5, 3}) task_schedule(t2, {5, 5.1})

t1_running = TRUE t2_running = TRUE

while t1_running or t2_running do if get_key() = 'q' then exit end if task_yield() end while

puts(1, "main task: stop\n") program ends when main task is finished </eucode>

Comparison with earlier multitasking schemes

In earlier releases of Euphoria, Language War already had a mechanism for multitasking, and some people submitted to User Contributions their own multitasking schemes. These were all implemented using plain Euphoria code, whereas this new multitasking feature is built into the interpreter. Under the old Language War tasking scheme a scheduler would *call* a task, which would eventually have to *return* to the scheduler, so it could then dispatch the next task.

In the new system, a task can call the built-in procedure task_yield at any point, perhaps many levels deep in subroutine calls, and the scheduler, which is now part of the interpreter, will be able to transfer control to any other task. When control comes back to the original task, it will resume execution at the statement after task_yield, with its call stack and all private variables intact. Each task has its own call stack, program counter (i.e. current statement being executed), and private variables. You might have several tasks all executing a routine at the same time, and each task will have its own set of private variable values for that routine. Global and local variables are shared between tasks.

It's fairly easy to take any piece of code and run it as a task. Just insert a few task_yield statements so it will not hog the CPU.

Comparison with multithreading

Not Categorized, Please Help

Search



Quick Links

User menu

Not signed in.

Misc Menu